home *** CD-ROM | disk | FTP | other *** search
- #include "Bilanz.h"
- #include <libraries/dosextens.h>
- #include <clib/console_protos.h>
-
- void GetMessage(void)
- {
- while (!(message = (struct IntuiMessage *) GetMsg(win->UserPort))) {
- Wait(1L << win->UserPort->mp_SigBit);
- }
- class = message->Class;
- code = message->Code;
- qualifier = message->Qualifier;
- adr = message->IAddress;
- ReplyMsg((struct Message *)message);
- }
-
- void GetMessageClass(void)
- {
- if (message = (struct IntuiMessage *) GetMsg(win->UserPort)) {
- class = message->Class;
- code = message->Code;
- qualifier = message->Qualifier;
- ReplyMsg((struct Message *)message);
- }
- }
-
- void WaitNoButton(void) /* waits until no button is pressed */
- {
- do GetMessageClass();
- while (qualifier & 0x6000L);
- }
-
- void Frame (long x1, long y1, long x2, long y2, long color)
- {
- SetAPen(RP,color); Move(RP,x1,y1);
- Draw(RP,x2,y1); Draw(RP,x2,y2);
- Draw(RP,x1,y2); Draw(RP,x1,y1);
- }
-
- void Print (char *s, long x, long y)
- {
- Move(RP, x, y);
- Text(RP, s, (long)strlen(s));
- }
-
-
- void ltos (long n, char *s, short w, short point, char fill)
- /* convert integer to string:
- w = width = stringlength
- point = 0 -> no dec-point
- > 0 -> dec-point insert at position <point>
- fill = character for leading zeros */
- {
- short i, minus = 0;
- i = w; point--;
- s[i--] = 0;
- if (point >= 0) s[point] = '.';
- if (n<0) { minus = 1; n = -n; }
- do {
- s[i--] = n % 10 + '0';
- if (point == i) i--;
- } while (((n /= 10) > 0) && (i >= 0));
- if (n>0) s[0] = '!';
- if (minus)
- if (i>=0) s[i--] = '-';
- else s[0] = '!';
- while (i>=0) {
- if (i == point) s[i] = '.';
- else if (i == (point-1)) s[i] = '0';
- else if ((i > point) && (point > 0)) s[i] = '0';
- else s[i] = fill;
- i--;
- }
- }
-
-
- double stof (unsigned char *s) /* convert string to real */
- {
- short i=0, minus=0, ok=1;
- double f=0.0, n=10.0;
- /* read whitespaces and minus */
- while (s[i] && !(s[i]>=48 && s[i]<=57 || s[i]=='.' || s[i]==',')) {
- if (s[i++]=='-') minus=1-minus; /* two minus = plus */
- }
- do { /* numbers befor decpoint */
- if (s[i]>=48 && s[i]<=57) f=f*10.0+(s[i]-48);
- else if (s[i]=='.' || s[i]==',') ok=2;
- else ok=0;
- i++;
- } while(ok==1);
- while (ok==2) { /* numbers behind decpoint */
- if (s[i]>=48 && s[i]<=57) f=f+(double)(s[i]-48)/n;
- else ok=0;
- i++; n*=10.0;
- }
- if (minus) f=-f;
- return(f);
- }
-
-
- long round (double real)
- {
- if (real < 0.0) return((long)(real - 0.5));
- else return((long)(real + 0.5));
- }
-
-
- #define UP 65
- #define DN 66
- #define LE 68
- #define RI 67
-
- short ReadText (unsigned char *s, long x, long y, short max, short startpos,
- char ins, unsigned char *abbruch,
- long f0, long f1, long f2, long f3)
- /*
- s = übergebener String
- x, y = Position auf dem Bildschirm
- max = es können maximal max Zeichen in s eingegeben werden; ist der
- übergebene String länger, wird der Rest abgeschnitten
- startpos= Position des Cursors im String bei Aufruf der Routine
- Ein Wert < 1 steht für letzte Position
- ins = 0 -> Overwrite-Modus
- = 1 -> Insert-Modus
- abbruch = String, der alle Abbruchwerte nach RAWKEY enthält (z.B.
- abbruch[] = {13,27,9,1,4,0};
- abbruch sollte mindestens das RETURN-Zeichen und
- die 0 als String-Abschluß enthalten.
- 1 im String steht für Abbruch bei Maustaste
- 2 " bei Erreichen der max. Länge
- 3 " bei CursorLeft/Right am
- Anfang bzw. Ende des Strings
- 4 " bei CursorUp/Down
- 5 " bei F-Tasten
- f0 = Hintergrundfarbe
- f1 = Zeichenfarbe
- f2 = Cursorfarbe
- f3 = Zeichenfarbe im Cursor
- Return-Wert ist der Tastatur-Code von Sondertasten, ansonsten 0
-
- changeflag ist eine globale Variable, um innerhalb des Programms
- festzustellen, ob Änderungen im String vorgenommen wurden.
- Die entsprechenden Zeilen können aber auch gelöscht werden.
- */
- {
- #define INPUTLEN 16L
- struct InputEvent inputEvent = { 0, IECLASS_RAWKEY,0, 0,0 };
- short i, j, asc, laenge = strlen(s), laengeabbr = strlen(abbruch);
- char str[2], fertig=0, mausabbruch=0, laengenabbruch=0,
- abbruchLeRi=0, abbruchUpDn=0, abbruchF=0, nop;
- byte inputPuffer[INPUTLEN];
-
- if (laenge>max) s[laenge]=0;
- i = (--startpos < 0) ? laenge : startpos;
- if (i >= max) i = max-1;
- SetAPen(RP,f1); SetBPen(RP,f0);
- Print(s,x,y);
- SetAPen(RP,f3); SetBPen(RP,f2);
- str[0]=(s[i]==0)? 32 : s[i]; str[1]=0;
- Move(RP,x+8*i,y); Text(RP,str,1L);
- /* Abbruchbedingungen registrieren */
- for (j=0; j<laengeabbr; j++) {
- if (abbruch[j]==1) mausabbruch=1;
- else if (abbruch[j]==2) laengenabbruch=1;
- else if (abbruch[j]==3) abbruchLeRi=1;
- else if (abbruch[j]==4) abbruchUpDn=1;
- else if (abbruch[j]==5) abbruchF=1;
- }
- do {
- GetMessage();
- nop=1;
- if ((class & RAWKEY) && !(code & IECODE_UP_PREFIX)) {
- inputEvent.ie_Code = code;
- inputEvent.ie_Qualifier = qualifier;
- if (RawKeyConvert(&inputEvent,inputPuffer,INPUTLEN,NULL) > 0) {
- asc = inputPuffer[0];
- nop = j = 0;
- while (j < laengeabbr && !fertig)
- if (asc==abbruch[j++]) fertig=1;
- if (!fertig) {
- if (asc>31 && asc<127 || asc==228 || asc==246 || asc==252 ||
- asc==223 || asc==196 || asc==214 || asc==220) {
- if (ins==1 && laenge<max) {
- j= ++laenge;
- while (j>i) { s[j]=s[j-1]; j--; }
- s[i]=asc;
- if ((i+1)<max) i++;
- else if (laengenabbruch) fertig=2;
- changeflag = 1;
- }
- else if (ins==0) {
- if (i==laenge) { laenge++; s[laenge]=0; }
- s[i]=asc;
- if ((i+1)<max) i++;
- else if (laengenabbruch) fertig=2;
- changeflag = 1;
- }
- }
- else if (asc==8 && i>0) {
- j= --i;
- while (j<laenge) { s[j]=s[j+1]; j++; }
- laenge--;
- changeflag = 1;
- }
- else if (asc==127 && i<laenge) {
- j=i;
- while(j<laenge) { s[j]=s[j+1]; j++; }
- laenge--;
- changeflag = 1;
- }
- else if (asc==155) {
- asc=inputPuffer[1];
- if (abbruchF && asc>=48 && asc<=57) fertig=1;
- else if (asc==RI && i<laenge && (i+1)<max) i++;
- else if (asc==RI && (i==laenge || i==max-1) && abbruchLeRi) fertig=1;
- else if (asc==LE && i>0) i--;
- else if (asc==LE && i==0 && abbruchLeRi) fertig=1;
- else if (abbruchUpDn && (asc==UP || asc==DN)) fertig=1;
- else if (asc==UP) i=0;
- else if (asc==DN) {
- if (laenge==max) i = max-1;
- else i = laenge;
- }
- }
- }
- }
- }
- else if (class & INTUITICKS) {
- SetBPen(RP,0); PrintDate();
- }
- /* Abbruch bei Drücken der linken Maustaste: */
- else if (qualifier & 0x4000) {
- fertig=1; asc=1, nop=0;
- }
- if (!nop) {
- SetAPen(RP,f1); SetBPen(RP,f0);
- Print(s,x,y);
- if (laenge<max) Print(" ",x+laenge*8,y);
- if (asc==8 && i==laenge) Print(" ",x+laenge*8+8,y);
- if (!fertig) { /* Cursor zeichnen */
- SetAPen(RP,f3); SetBPen(RP,f2);
- str[0]=(s[i]==0)? 32 : s[i]; str[1]=0;
- Move(RP,x+8*i,y); Text(RP,str,1L);
- }
- }
- } while (!fertig);
- SetAPen(RP,1L); SetBPen(RP,0);
- if (fertig==2) return(0);
- else return(asc);
- }
-
-
- void GetClock(void)
- { unsigned char clock[8];
- getclk(clock);
- time.hour = clock[4];
- time.min = clock[5];
- time.sec = clock[6];
- time.day = clock[3];
- time.month= clock[2];
- time.year = clock[1] + 1980;
- strcpy(time.weekday,Weekday[clock[0]]);
- }
-
- void PrintTime(short x, short y)
- { unsigned char s[5], o=0;
- SetAPen(RP,1L);
- ltos(time.hour,s,2,0,' '); Print(s,x,y+10); Print(":",x+16,y+10);
- ltos(time.min,s,2,0,'0'); Print(s,x+24,y+10); Print(":",x+40,y+10);
- ltos(time.sec,s,2,0,'0'); Print(s,x+48,y+10);
- Print(time.weekday,x,y);
- Print(",",x+16,y);
- ltos(time.day,s,2,0,' ');
- if (s[0]!=' ') x += 8;
- Print(s,x+24,y); Print(".",x+40,y);
- ltos(time.month,s,2,0,' ');
- if (s[0]==' ') { s[0] = s[1]; s[1] = 0; }
- else o = 8;
- Print(s,x+48,y); Print(".",x+56+o,y);
- ltos(time.year,s,4,0,' '); s[0]=s[2]; s[1]=s[3]; s[2]=0; Print(s,x+64+o,y);
- }
-
- void PrintDate(void)
- {
- GetClock(); PrintTime(535,23);
- }
-
-
- void StatusClr(void) /* clear statusline */
- { int i;
- for (i=1; i<=13; i++) {
- ScrollRaster(RP,0L,1L,70L,82L,630L,94L);
- Delay(1);
- }
- }
-
- void StatusText(char s[200], char modus, long color) /* print text in statusline */
- { int i;
- status = 1; counter = 30;
- if (strlen(s) > 69) s[69]=0;
- if (modus) {
- for (i=1; i<=12; i++) {
- ScrollRaster(RP,0L,1L,70L,82L,630L,94L);
- Delay(1);
- }
- }
- SetAPen(RP,color);
- Print(s,72,91);
- if (modus) Delay((modus-1)*60);
- }
-
- int power2(int i)
- { int n=1;
- while(i>0) {n *= 2; i--; }
- return(n);
- }
-
-
- short InsertEntry(struct Entry entry, int month)
- {
- struct Entry *new, *cur, *old;
- if (!(new = (struct Entry *) AllocMem(sizeof(struct Entry),0))) return(0);
- strcpy(new->area,entry.area);
- strcpy(new->title,entry.title);
- new->date = entry.date;
- new->costs = entry.costs;
- new->next = 0;
- old = cur = year->month[month].first;
- if (!cur) year->month[month].first = new; /* first entry */
- else {
- while (cur->date <= new->date && cur->next) {
- old = cur; cur = cur->next;
- }
- if (new->date < cur->date) { /* insert entry before current */
- if (old==cur) { /* new entry is the first in the list*/
- year->month[month].first = new;
- new->next = cur;
- }
- else {
- old->next = new;
- new->next = cur;
- }
- }
- else { /* append entry */
- cur->next = new;
- }
- }
- return(1);
- }
-
- void DeleteEntry(struct Entry *entry)
- {
- struct Entry *old, *cur;
- old = cur = year->month[curmonth].first;
- while (cur != entry) {
- old = cur; cur = cur->next;
- }
- if (old == cur) year->month[curmonth].first = cur->next;
- else old->next = cur->next;
- FreeMem(cur,sizeof(struct Entry));
- }
-
- short InsertYear(struct Year **ptr)
- { short n;
- struct Year *new, *cur, *old;
- cur = firstyear; old = 0;
- while (cur && cur->year < curyear) { old = cur; cur = cur->next; }
- if (cur && cur->year == curyear) return(-1); /* year exists already */
- new = (struct Year *) AllocMem(sizeof(struct Year),0);
- if (new) {
- new->year = curyear;
- new->income = new->expend = 0;
- for (n=1; n<=12; n++) {
- new->month[n].income = new->month[n].expend = new->month[n].number = 0;
- new->month[n].first = 0;
- }
- if (!old) { new->next = firstyear; firstyear = new; }
- else { old->next = new; new->next = cur; }
- *ptr = new;
- return(1);
- }
- else return(0);
- }
-
- void DeleteYears(void) /* free memory */
- {
- struct Year *nextyear;
- struct Entry *entry, *nextentry;
- year = firstyear;
- while (year) {
- nextyear = year->next;
- for (curmonth=1; curmonth<=12; curmonth++) {
- entry = year->month[curmonth].first;
- while (entry) {
- nextentry = entry->next;
- FreeMem(entry,sizeof(struct Entry));
- entry = nextentry;
- }
- }
- FreeMem(year,sizeof(struct Year));
- year = nextyear;
- }
- }
-
- int MouseIn(int x1, int y1, int x2, int y2) /* mouse in frame ?*/
- {
- if (win->MouseX >= x1 && win->MouseX <= x2 &&
- win->MouseY >= y1 && win->MouseY <= y2)
- return(1);
- else return(0);
- }
-
- int MouseInFrame(int x1, int y1, int x2, int y2) /* mouse pressed in frame ?*/
- {
- if (qualifier & 0x4000L &&
- win->MouseX >= x1 && win->MouseX <= x2 &&
- win->MouseY >= y1 && win->MouseY <= y2)
- return(1);
- else return(0);
- }
-
-
- void Gadget(unsigned char *s, int x1, int y1, int x2, int y2, int on)
- /* draws own gadgets because updating is much faster (and easier)
- draws frame around x1,y1,x2,y2
- on = 1 => gadget is pressed
- on = 0 => gadget off */
- {
- SetAPen(RP, 2L - on);
- Move(RP,x1-2,--y1); Draw(RP,x1-2,y2+1); Draw(RP,--x1,y2);
- Draw(RP,x1,y1); Draw(RP,++x2,y1);
- SetAPen(RP, 1L + on);
- Move(RP,x2+1,++y2); Draw(RP,x2+1,y1); Draw(RP,x2,++y1);
- Draw(RP,x2,y2); Draw(RP,x1,y2);
- Print(s, (x1+x2+1)/2 - strlen(s)*4, (y1+y2)/2 + 3);
- }
-
- int FilterCmp(unsigned char s[41]) /* check if text in filter */
- { int i, fn=1;
- for (i=0; i<FILTER; i++) {
- if ((filter & fn) && strcmp(year->filtertext[i],s)==0) {
- if (include) return(1);
- else return(0);
- }
- fn *= 2;
- }
- if (include) return(0);
- else return(1);
- }
-
- void UpdateFilter(int nr)
- /* nr = -1 => update all filter
- nr >= 0 => update filter no. nr */
- {
- int i, d;
- i = (nr < 0) ? 0 : nr;
- do {
- d = i * 74 + 64;
- SetAPen(RP,0L);
- RectFill(RP,d,66L,d+69L,76L);
- Gadget(year->filtername[i], d,66,d+69,76, filterflag[i]);
- i++;
- } while (nr < 0 && i<FILTER);
- }
-
- void FilterSwitch(int nr) /* filter on/off */
- {
- if (filterflag[nr]) {
- filter &= 65535-power2(nr);
- filterflag[nr] = 0;
- }
- else {
- filter |= power2(nr);
- filterflag[nr] = 1;
- }
- UpdateFilter(nr);
- }
-
- void FilterChange(int nr) /* change text of filter */
- { int line=0, ok=1, w;
- OutClr(); SetAPen(RP,1L);
- Print("Filtereintrag ändern:",50,120);
- Print("Gadget-Name:",50,130);
- Print("Filter-Text:",50,140); Print(year->filtertext[nr],150,140);
- while (ok) {
- if (!line) {
- w = ReadText(year->filtername[nr],150,130,8,1,INS,stopRead,0,1,2,1);
- if (w == 27) ok = 0;
- if (w != 1) line = 1;
- }
- else {
- w = ReadText(year->filtertext[nr],150,140,20,1,INS,stopRead,0,1,2,1);
- if (w == 65) line = 0;
- else if (w == 1) line = 1;
- else ok = 0;
- }
- }
- UpdateFilter(nr);
- OutStart();
- StatusYear(1);
- }
-
-
- int Count(int step)
- /* step back/forward one entry (depend on filter) or
- jump to first/last entry */
- {
- if (step<=0 && !count) return(0);
- else if (step==2) { if (number-MAXLINE <= 0) return(0); }
- if (!filter) {
- if (step==0) { count = 0; return(1); }
- else if (step==2) {
- if (count < number-MAXLINE) {
- count = number-MAXLINE; return(1);
- }
- else return(0);
- }
- else if (step==1) {
- if (number-count-MAXLINE <= 0) return(0);
- }
- count += step; return(1);
- }
- else {
- struct Entry *entry;
- int i, j;
- entry = year->month[curmonth].first;
- if (step==0) {
- i = 0;
- while (entry && !FilterCmp(entry->area)) {
- entry = entry->next;
- i++;
- }
- if (count!=i) { count = i; return(1); }
- else return(0);
- }
- else if (step==-1) {
- j = -1;
- for (i=0; i<count; i++) {
- if (FilterCmp(entry->area)) j = i;
- entry = entry->next;
- }
- if (j>=0) { count = j; return(1); }
- else return(0);
- }
- else if (step==1) {
- j = 0;
- for (i=0; i<=count; i++) {
- if (FilterCmp(entry->area)) j++;
- entry = entry->next;
- }
- if (number-j-MAXLINE < 0) return(0);
- while (entry && !FilterCmp(entry->area)) {
- entry = entry->next;
- i++;
- }
- if (entry) { count = i; return(1); }
- else return(0);
- }
- else { /* step==2 */
- j = number-MAXLINE; i = 0;
- while (j) {
- if (FilterCmp(entry->area)) j--;
- entry = entry->next; i++;
- }
- while (!FilterCmp(entry->area)) {
- entry = entry->next; i++;
- }
- if (count!=i) { count = i; return(1); }
- else return(0);
- }
- }
- }
-
-
- short LoadData(unsigned char *filename)
- {
- FILE *fp;
- int i, j, n, mon, ok = 1;
- unsigned char s[100], date[6];
- struct Entry entry;
- /* form of file :
- filtername1,filtername2,..., (max. 8)
- filtertext1,...
- tt.mm,area1
- 12345,title1
- tt.mm,area2 ... */
- for (i=0; i<FILTER; i++) year->filtername[i][0] = year->filtertext[i][0] = 0;
- if (fp = fopen(filename,"r")) {
- SetAPen(RP,0L); RectFill(RP,70L,82L,630L,94L);
- strcpy(s,"Lade "); strcat(s,filename); strcat(s," ");
- StatusText(s,0,1);
- fgets(s,sizeof(s),fp); s[strlen(s)-1] = 0;
- j = n = 0;
- for (i=0; i<=strlen(s); i++) {
- if (s[i] == ',' || s[i] == 0) {
- year->filtername[n][j] = 0; j = 0;
- if (n<FILTER) n++;
- }
- else if (j<8) year->filtername[n][j++] = s[i];
- }
- fgets(s,sizeof(s),fp); s[strlen(s)-1] = 0;
- j = n = 0;
- for (i=0; i<=strlen(s); i++) {
- if (s[i] == ',' || s[i] == 0) {
- year->filtertext[n][j] = 0; j = 0;
- if (n<FILTER) n++;
- }
- else if (j<20) year->filtertext[n][j++] = s[i];
- }
- do {
- n = fscanf(fp,"%5s,",date);
- if (n==1) {
- fgets(s,sizeof(s),fp); s[strlen(s)-1] = 0;
- if (strlen(s) <= 20) strcpy(entry.area,s);
- else { ok = 0; StatusText("Fehler: zu langer Bereichsname!",2,2); }
- n = fscanf(fp,"%ld,",&entry.costs);
- if (n != 1) ok = 0;
- fgets(s,sizeof(s),fp); s[strlen(s)-1] = 0;
- if (strlen(s) <= 40) strcpy(entry.title,s);
- else { ok = 0; StatusText("Fehler: zu langer Titel!",2,2); }
- if (ok) {
- s[0] = date[0]; s[1] = date[1]; s[2] = 0; /* read day */
- entry.date = atoi(s);
- s[0] = date[3]; s[1] = date[4]; s[2] = 0; /* read month */
- mon = atoi(s);
- if (mon > 0 && mon <= 12) {
- if (ok = InsertEntry(entry,mon)) {
- if (entry.costs > 0) year->month[mon].income += entry.costs;
- else year->month[mon].expend += labs(entry.costs);
- year->month[mon].number++;
- }
- else StatusText("Nicht genug Speicherplatz!",1,2);
- }
- else { ok = 0; StatusText("Fehler: Falscher Monat!",2,2); }
- }
- if (!ok) {
- strcpy(s,"Habe "); strcat(s,filename);
- strcat(s," nicht vollständig geladen!"); StatusText(s,1,2);
- }
- }
- else {
- ok = 0;
- strcpy(s,"Habe "); strcat(s,filename); strcat(s," geladen");
- StatusText(s,0,1);
- }
- } while (ok);
- fclose(fp);
- for (n=1; n<=12; n++) {
- year->income += year->month[n].income;
- year->expend += year->month[n].expend;
- }
- return(1);
- }
- else {
- strcpy(s,"Kann "); strcat(s,filename); strcat(s," nicht öffnen!");
- StatusText(s,1,1); return(0);
- }
- }
-
- short SaveData(unsigned char *filename)
- {
- FILE *fp;
- struct Entry *entry;
- unsigned char bakfile[100], s[200], date[6], mon, i;
- date[2] = '.'; date[5] = 0;
- strcpy(bakfile,filename);
- strcat(bakfile,".bak");
- StatusClr();
- if (DeleteFile(bakfile)) {
- strcpy(s,"Lösche "); strcat(s,bakfile); StatusText(s,0,1); Delay(10);
- }
- if (Rename(filename,bakfile)) {
- strcpy(s,"Rename "); strcat(s,filename); strcat(s," as ~.bak");
- StatusText(s,0,1); Delay(10);
- }
- if (fp = fopen(filename,"w")) {
- strcpy(s,filename); strcat(s," speichern..."); StatusText(s,1,1);
- for (i=0; i<FILTER-1; i++) fprintf(fp,"%s,",year->filtername[i]);
- fprintf(fp,"%s\n",year->filtername[FILTER-1]);
- for (i=0; i<FILTER-1; i++) fprintf(fp,"%s,",year->filtertext[i]);
- fprintf(fp,"%s\n",year->filtertext[FILTER-1]);
- for (mon=1; mon<=12; mon++) {
- ltos(mon,s,2,0,'0'); date[3] = s[0]; date[4] = s[1];
- entry = year->month[mon].first;
- while (entry) {
- ltos(entry->date,s,2,0,'0'); date[0] = s[0]; date[1] = s[1];
- fprintf(fp,"%5s,%s\n%8ld,%s\n",date,entry->area,
- entry->costs,entry->title);
- entry = entry->next;
- }
- }
- fclose(fp);
- strcpy(s,filename); strcat(s," gespeichert "); StatusText(s,0,1);
- return(1);
- }
- else {
- strcpy(s,"Kann "); strcat(s,filename); strcat(s," nicht öffnen!");
- StatusText(s,1,2); Delay(200); return(0);
- }
- }
-